package com.enlorenz.support.bank.iso8583.comu;

import com.enlorenz.common.util.TimeConstants;
import com.enlorenz.core.channel.socket.common.reader.LengthBodySocketReader;
import com.enlorenz.core.channel.socket.common.translate.BytesMessageTransformer;
import com.enlorenz.core.channel.socket.common.writer.DefaultSocketWriter;
import com.enlorenz.core.channel.socket.imp.shortconnection.SocketClientChannel;
import com.enlorenz.support.bank.iso8583.Iso8583Constants;

/**
 * iso8583数据发送者
 * @author Karma
 */
public class Iso8583SocketSender {
	private SocketClientChannel<byte []> clientChannel;
	//头长度
	private Integer headLength=
			Iso8583Constants.DEFAULT_HEAD_LENGTH;
	private long timeOut=TimeConstants.SECOND_MILLI*120;
	//ip地址
	private String ip;
	//端口
	private int port;
	
	/**
	 * 初始化
	 */
	public void init(){
		clientChannel=new SocketClientChannel<byte []>();
		clientChannel.setSocketTimeout(timeOut);
		clientChannel.setIp(ip);
		clientChannel.setPort(port);
		clientChannel.setMessageBytesTransformer(new BytesMessageTransformer());
		clientChannel.setSocketWriter(new DefaultSocketWriter());
		
		LengthBodySocketReader lbsr=new LengthBodySocketReader();
		lbsr.setCode("utf-8");
		lbsr.setTrimLength(false);
		lbsr.setHeadLength(headLength);
		clientChannel.setSocketReader(lbsr);
	}
	
	/**
	 * 发送数据
	 * @param data 数据
	 * @return 返回的数据
	 */
	public byte[] send(byte [] data){
		return (byte[])clientChannel.reqResMsgHandle(data);
	}
	

	public SocketClientChannel<byte[]> getClientChannel() {
		return clientChannel;
	}

	public void setClientChannel(SocketClientChannel<byte[]> clientChannel) {
		this.clientChannel = clientChannel;
	}

	public long getTimeOut() {
		return timeOut;
	}

	public void setTimeOut(long timeOut) {
		this.timeOut = timeOut;
	}

	public Integer getHeadLength() {
		return headLength;
	}

	public String getIp() {
		return ip;
	}

	public int getPort() {
		return port;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	public void setHeadLength(Integer headLength) {
		this.headLength = headLength;
	}
}
